package models

import (
	"time"

	"github.com/astaxie/beego/orm"
)

type Xiangmu struct {
	Id                       int
	Title                    string        `ht:"search;list;name(标题)"`
	Href                     string                `ht:"name(链接);"`
	SEOKeyWords              string        `ht:"name(SEO关键词)"`
	SEOKeyContent            string        `orm:"type(text)" ht:"name(SEO关键内容)"`
	BasicPrice               int                `ht:"list;name(基本价格)"`
	PriceDescription         string        `orm:"type(text)"`
	KeyWords                 string        `ht:"name(关键词)"`
	ListPic                  string        `ht:"name(列表页的图片);type(pic)"`
	DetailPics               string        `orm:"type(text)" ht:"name(详情页图片集);hide"`
	PublishTime              time.Time        `ht:"list;name(发布时间);defaultvalue(time.Now())"`
	EditTime                 time.Time        `ht:"list;name(编辑时间);defaultvalue(time.Now())"`
	ProdectPics              string        `orm:"type(text)" ht:"name(产品图片集);type(pic);hide"`
	ProdectDescritionPic     string        `ht:"name(产品描述图片);type(pic);hide"`
	TravelRoutePics          string        `orm:"type(text)" ht:"name(路线介绍图片集);type(pic);hide"`
	TravelRouteDescritionPic string        `ht:"name(路线描述图片);type(pic);hide"`
	HotelPic                 string        `ht:"name(酒店介绍图片);hide"`
	FoodPic                  string        `ht:"name(饮食介绍图片);hide"`
	WatchCount               int64        `ht:"list;name(浏览人数)"`
	StarCount                int64        `ht:"list;name(点赞人数)"`
	HotStatus                int                `ht:"list;name(是否热门);enum([{\"Key\":\"0\",\"Value\":\"热门\"},{\"Key\":\"1\",\"Value\":\"非热门\"}])"`
	Status                   int                `ht:"name(状态);status([{\"Num\":0,\"Name\":\"未审核\"},{\"Num\":1,\"Name\":\"删除\"},{\"Num\":2,\"Name\":\"发布\"}])"`
}

func init() {
	orm.RegisterModel(new(Xiangmu))
}
func (*Xiangmu)TableName() string {
	return "xiangmu"
}

func (*Xiangmu) FN_Id() string {
	return "Id"
}
func (*Xiangmu) FN_Title() string {
	return "Title"
}
func (*Xiangmu) FN_Href() string {
	return "Href"
}
func (*Xiangmu) FN_SEOKeyWords() string {
	return "SEOKeyWords"
}
func (*Xiangmu) FN_SEOKeyContent() string {
	return "SEOKeyContent"
}
func (*Xiangmu) FN_BasicPrice() string {
	return "BasicPrice"
}
func (*Xiangmu) FN_PriceDescription() string {
	return "PriceDescription"
}
func (*Xiangmu) FN_KeyWords() string {
	return "KeyWords"
}
func (*Xiangmu) FN_ListPic() string {
	return "ListPic"
}
func (*Xiangmu) FN_DetailPics() string {
	return "DetailPics"
}
func (*Xiangmu) FN_PublishTime() string {
	return "PublishTime"
}
func (*Xiangmu) FN_EditTime() string {
	return "EditTime"
}
func (*Xiangmu) FN_ProdectPics() string {
	return "ProdectPics"
}
func (*Xiangmu) FN_ProdectDescritionPic() string {
	return "ProdectDescritionPic"
}
func (*Xiangmu) FN_TravelRoutePics() string {
	return "TravelRoutePics"
}
func (*Xiangmu) FN_TravelRouteDescritionPic() string {
	return "TravelRouteDescritionPic"
}
func (*Xiangmu) FN_HotelPic() string {
	return "HotelPic"
}
func (*Xiangmu) FN_FoodPic() string {
	return "FoodPic"
}
func (*Xiangmu) FN_WatchCount() string {
	return "WatchCount"
}
func (*Xiangmu) FN_StarCount() string {
	return "StarCount"
}
func (*Xiangmu) FN_HotStatus() string {
	return "HotStatus"
}
func (*Xiangmu) FN_Status() string {
	return "Status"
}

const (
	XiangmuInit Status = 0 + iota
	XiangmuDelete
	XiangmuPublish
)

func GetXiangmuList(xiangmu *[]Xiangmu, offset int, limit int) error {
	o := orm.NewOrm()
	m := Xiangmu{}
	fields := []string{"Id", "Title", "Href", "SEOKeyWords", "SEOKeyContent", "BasicPrice", "PriceDescription", "KeyWords", "ListPic", "DetailPics", "PublishTime", "EditTime"}
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_Status(), XiangmuPublish).OrderBy("-" + m.FN_EditTime()).Offset(offset).Limit(limit).All(xiangmu, fields...)
	return err
}
func GetXiangmuListByTitle(xiangmu *[]Xiangmu, offset int, limit int, search string) error {
	o := orm.NewOrm()
	m := Xiangmu{}
	fields := []string{"Id", "Title", "Author", "SEOKeyWords", "SEOKeyContent", "Short", "ListPic", "DetailPic", "PublishTime", "EditTime", "WatchCount", "StarCount", "HotStatus", "Status", }
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_Status(), XiangmuPublish).Filter(m.FN_Title() + "__icontains", search).OrderBy("-" + m.FN_EditTime()).Offset(offset).Limit(limit).All(xiangmu, fields...)
	return err
}
func GetXiangmuSearchCount(search string) (int64, error) {
	o := orm.NewOrm()
	m := Xiangmu{}
	count, err := o.QueryTable(m.TableName()).Filter(m.FN_Status(), XiangmuPublish).Filter(m.FN_Title() + "__icontains", search).Count()
	return count, err
}
func GetXiangmuListCount() (int64, error) {
	o := orm.NewOrm()
	m := Xiangmu{}
	count, err := o.QueryTable(m.TableName()).Filter(m.FN_Status(), XiangmuPublish).Count()
	return count, err
}

func GetXiangmuHotList(xiangmu *[]Xiangmu, offset int, limit int) error {
	o := orm.NewOrm()
	m := Xiangmu{}
	fields := []string{"Id", "Title", "Author", "SEOKeyWords", "SEOKeyContent", "Short", "ListPic", "DetailPic", "PublishTime", "EditTime", "WatchCount", "StarCount", "HotStatus", "Status", }
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_Status(), XiangmuPublish).OrderBy("-" + m.FN_HotStatus()).Offset(offset).Limit(limit).All(xiangmu, fields...)
	return err
}

func GetXiangmuDetail(xiangmu *Xiangmu) error {
	o := orm.NewOrm()
	return o.Read(xiangmu)
}

func AddXiangmuBrowseCount(xiangmu *Xiangmu) error {
	o := orm.NewOrm()
	_, err := o.QueryTable(xiangmu.TableName()).Filter(xiangmu.FN_Id(), xiangmu.Id).Update(orm.Params{
		xiangmu.FN_WatchCount(): orm.ColValue(orm.ColAdd, 1),
	})
	return err
}

func AddXiangmuStarCount(xiangmu *Xiangmu) error {
	o := orm.NewOrm()
	_, err := o.QueryTable(xiangmu.TableName()).Filter(xiangmu.FN_Id(), xiangmu.Id).Update(orm.Params{
		xiangmu.FN_StarCount(): orm.ColValue(orm.ColAdd, 1),
	})
	return err
}

